package com.lsa.letcode.the38.countAndSay;

public class Solution {
    public String countAndSay(int n) {
    	String s = "1";
    	for (int i = 0; i < n-1; i++) {
			s = next(s);
		}
    	
    	return s;
    }
    
    static String next(String s) {
    	StringBuilder sb = new StringBuilder();
    	char prev = s.charAt(0);
    	int count = 0;
    	for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (c != prev) {
				sb.append(count);
				sb.append(prev);
				count = 1;
				prev = c;
			}
			else {
				count++;
			}
		}
    	sb.append(count);
    	sb.append(prev);
    	return sb.toString();
    }
}